package com.example.demo.web;

import com.alibaba.fastjson.parser.ParseContext;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpRequest;
import com.aliyuncs.http.HttpResponse;
import com.example.demo.domain.User;
import com.example.demo.register.Authcode;
import com.example.demo.register.SmsDemo;
import com.example.demo.service.Userservice;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import ch.qos.logback.core.net.SyslogOutputStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * HelloController类描述:
 *
 *
 *username :用户名
 *password:密码
 *
 * @author yangzhenlong
 * @since 2016/11/29
 */
@RestController
@RequestMapping("/LoginRegister")
public class LoginandRegiste {

	@Autowired
	private Userservice userservice;

	
	private  static String phoneid;

	/**
	 * 发送验证码
	 * @param userid
	 * @return 
	 * @throws ClientException
	 * @throws InterruptedException
	 */
	@ApiOperation(value="根据手机号发送验证码(会检查手机号格式)",httpMethod="POST",notes="phonenum"+"{\"phonenum\":\"手机号\"}")
	@PostMapping("/sendcode")
	@RequestMapping(value="/sendcode",method={RequestMethod.POST})
	public String sendcode(@ApiParam @RequestBody Map<String,Object> params ) throws ClientException, InterruptedException{
		
		String userid = params.get("phonenum").toString();  
		
		
		//检查手机号格式
		
        if(CheckPhonenum.isMobileNO(userid)==false){
			
			return	"手机号格式不正确";
		} 
		
 		//绑定手机号
 		phoneid=userid;
		
 		//发送验证码
		SmsDemo.setPhonenum(userid);
		String  code=Authcode.getAutocode();
		SmsDemo.authcode=code;
		SmsDemo .main(null);
		
        System.out.println(code);
		
		return  "已发送";
	}
	
      //手机号注册
	@ApiOperation(value="手机用户注册(会检查格式和是否已注册)",httpMethod="POST",notes="{\"username\": \"231616\",\"code\":\"025165\", \"password\":\"1221\"}手机号注册默认用户名为手机号,可以修改")
	@PostMapping("/registerbyphone")
	@RequestMapping(value="/registerbyphone",method=RequestMethod.POST)
	public String add(@RequestBody Map<String ,Object> param) throws ClientException, InterruptedException {
		String userid=param.get("username").toString ();
		String code=param.get("code").toString ();
		String userpassword=param.get("password").toString ();
		  //验证手机号格式
       if(CheckPhonenum.isMobileNO(userid)==false){
			
			return	"手机号格式不正确";
		} 
       //检查验证是否一致
		String code2=SmsDemo.authcode;
		if(code2.endsWith(String.valueOf(code))){
			
			  int num = userservice.insertUser(userid, userpassword);
				if (num == 1) {
		          
					return "注册成功";

				} else {

					return "此用户已被注册";
				}
		}else{
			
			return "验证码错误";
		}

	}

    //普通注册
	@ApiOperation(value="用户名注册(检查用户名是否重复,暂支持 4-16数字字母组合)",httpMethod="POST",notes=" {\"username\":\"12321\",  \"password\":\" 234534\"}")
	@PostMapping("/registerbyname")
	@RequestMapping(value="/registerbyname",method = RequestMethod.POST)
	public String subreg(@ApiParam("用户名--密碼") @RequestBody Map<String ,Object> param) {
		
	String userid=param.get("username").toString();
			String userpassword=param.get("password").toString();
		
		if(CheckPhonenum.isusernameNO(userid)==false){
			
			return	"用户名格式不正确,请重新输入";
		}
		int num =userservice.loginuser(userid, userpassword);
		if (num == 1) {

			return "此用户已被注册";
		} else {

			return "注册成功";
		}

	}
	
	
	
	@ApiOperation(value="用户登录(验证密码)",httpMethod="POST" ,notes="{\"username\":\"12321312312\",\"password\":\" 13232\"}")
	@PostMapping("/login")
	@RequestMapping(value="/login",method = RequestMethod.POST)
	public String sub( @RequestBody Map<String,Object> params ) {

	            String userid = params.get("username").toString();  
	            String userpassword =params.get("password").toString(); 
	      System.out.println(userid);
		
		
		int num = userservice.loginuser(userid, userpassword);
		if (num == 1) {

			return "登录成功";
		} else {

			return "用户名或密码不正确";
		}
	

	}
	
	@ApiOperation(value="查询所有User",httpMethod="GET")
	@PostMapping("/user")
	@RequestMapping(value="/user",method = RequestMethod.GET)
	public List<User> searchallUser(){
		
		List<User> pos= userservice.serchall();
		
		
		return  pos;
	}
	@ApiOperation(value="查询单个User",httpMethod="GET",notes=" {\"username\":\" 155841161566\"}")
	@PostMapping("/user/{username}")
	@RequestMapping(value="/user/{username}",method = RequestMethod.GET)
	public User searchOneUser(@ApiParam("用户名")@RequestParam String username){
		
		//String userid=param.get("userid").toString();
		
		System.out.println(username);
		User pos= userservice.serchOne(username);
		
		return  pos;
	}
	
/*	// static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

	    @ApiOperation(value="获取用户列表", notes="得到所有的用户信息")
	    @RequestMapping(value={"users"}, method=RequestMethod.GET)
	    public List<User> getUserList() {
	        List<User> r = new ArrayList<User>();
	      r=userservice.serchall();
	 System.out.println(r);
	        return r;
	    }*/
	    
	    @ApiOperation(value="忘记密码",httpMethod="PUT",notes="{\"code\":\"验证码\",\"userpwd\":\"112323\",\"userpassword\":\"112323\"}")
		@PostMapping("/resetpwd")
	    @RequestMapping(value="/resetpwd",method={RequestMethod.PUT})
		public String resetpwd(@RequestBody Map<String,Object> params) {
	    	String code = params.get("code").toString();  
	    	String userpwd = params.get("userpwd").toString();  
	    	String userpassword = params.get("userpassword").toString();  
	    	System.out.println(code+"//"+userpwd+"//"+userpassword);
	    	if(SmsDemo.authcode==null){
	    		
	    		return "请发送验证码";
	    	}
	    	
	    	if(SmsDemo.authcode.equals(code))	{
	       	
			if(userpwd.equals(userpassword)){
				
				userservice.insertUser(phoneid, userpwd);
				
				return "重置成功";
				
			}
			else{
			
				return "密码输入不一致,请重新输入";
			}
	    	
	    }else{
	    	return "验证码错误";
	    }
		}    
	    
		@ApiOperation(value="删除单个User",httpMethod="DELETE",notes="根据用户名删 {\"username\":\"180564651\"}")
		@PostMapping("/User/{username}")
		@RequestMapping(value="/user/{username}",method={RequestMethod.DELETE})
		public String deleteOneUser(@ApiParam("用户名")@RequestBody Map<String ,Object> param ){
			
			String userid=param.get("username").toString();
			
		
			
			//查询是否存在
			int a =userservice.selectbyid(userid);
			if(a>0){
				//删除
				  userservice.deleteUser(userid );
					
					return  "删除成功";
				
			}
			return "此用户不存在";
		
		}
 
}
